import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import image from "@rollup/plugin-image";
import globals from "rollup-plugin-node-globals";
import builtins from "rollup-plugin-node-builtins";
import { terser } from "rollup-plugin-terser";
import babel from "@rollup/plugin-babel";

// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = !process.env.ROLLUP_WATCH;

export default {
  input: "src/main.ts",
  output: {
    file: "public/bundle.js",
    format: "iife", // immediately-invoked function expression — suitable for <script> tags
    sourcemap: true,
  },
  plugins: [
    babel({
      extensions: [".js", ".ts"],
      exclude: "node_modules/**",
      babelHelpers: "bundled",
    }),
    resolve({
      extensions: [".js", ".ts"],
    }), // tells Rollup how to find date-fns in node_modules
    commonjs({
      extensions: [".js", ".ts"],
    }), // converts date-fns to ES modules
    json(),
    image(),
    globals(),
    builtins(),
    production && terser(), // minify, but only in production
  ],
};
